package com.xjscrm.console.common.web;
import com.xjscrm.common.utils.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 过滤器，加载重写body参数的过滤器
 * @author liuqi
 * @date 2021年08月14日 20:05
 */
public class UserInfoFilter implements Filter {
    private Logger logger = LoggerFactory.getLogger(UserInfoFilter.class);
    @Override
    public void doFilter(ServletRequest req, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        try {
            if(req instanceof HttpServletRequest){
                HttpServletRequest request  = (HttpServletRequest)req;
                String contentType = Tools.getStr(request.getContentType());

                if (contentType.equalsIgnoreCase(MediaType.APPLICATION_JSON_UTF8_VALUE)
                        || contentType.equalsIgnoreCase(MediaType.APPLICATION_JSON_VALUE)) {
                    //将参数放入重写的方法中
                    CustomHttpServletRequestWrapper customHttpServletRequestWrapper = new CustomHttpServletRequestWrapper(request);
                    chain.doFilter(customHttpServletRequestWrapper, response);
                }else if (contentType.equalsIgnoreCase(MediaType.APPLICATION_FORM_URLENCODED_VALUE)
                        || contentType.equalsIgnoreCase(MediaType.MULTIPART_FORM_DATA_VALUE)) {
                    RequestParameterWrapper requestParameterWrapper = new RequestParameterWrapper(request);
                    chain.doFilter(requestParameterWrapper, response);
                }else{//其他提交全部当作form表单提交参数
                    RequestParameterWrapper requestParameterWrapper = new RequestParameterWrapper(request);
                    chain.doFilter(requestParameterWrapper, response);
                }
            }else{
                chain.doFilter(req, response);
            }
        }catch (Exception e){
            logger.warn("customHttpServletRequestWrapper Error:", e);
            chain.doFilter(req, response);
        }
    }


    //获取Request的body数据
    private String getBody(ServletRequest request) {
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bufferedReader = null;
        InputStream inputStream = null;
        try {
            inputStream = request.getInputStream();
            if (inputStream != null) {
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                char[] charBuffer = new char[128];
                int bytesRead = -1;
                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                    stringBuilder.append(charBuffer, 0, bytesRead);
                }
            } else {
                stringBuilder.append("");
            }
        } catch (IOException ex) {

        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return stringBuilder.toString();
    }

}
